package com.cxg.fairy.ec.main.personal.favor;

import android.os.Handler;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.ec.detail.IGoodsDetailDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.MultipleRecyclerAdapter;
import com.cxg.fairy.ui.recycler.MultipleViewHolder;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.toasty.Toasty;
import com.diabin.latte.ec.R;
import java.math.BigDecimal;
import java.util.List;


/**
 * Created by Administrator on 2018/3/8.
 */

public class FavorAdapter extends MultipleRecyclerAdapter {

    private FavorDelegate favorDele = null;

    public void setFavorDelegate(FavorDelegate favorDele) {
        this.favorDele = favorDele;
    }

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    public FavorAdapter(List<MultipleItemEntity> data) {
        super(data);
        addItemType(FavorItemType.FAVOR_ITEM,R.layout.item_favor);
    }

    @Override
    protected void convert(final MultipleViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);

        switch (holder.getItemViewType()) {
            case FavorItemType.FAVOR_ITEM:
                final int favoriteId = entity.getField(FavorItemFields.ID);
                final int goodsId = entity.getField(FavorItemFields.GOODS_ID);
                final String title = entity.getField(FavorItemFields.GOODS_NAME);
                final String unit = entity.getField(FavorItemFields.GOODS_UNIT);
                final BigDecimal price = entity.getField(FavorItemFields.GOODS_PRICE);
                final String img = entity.getField(FavorItemFields.IMAGE_URL);


                final LinearLayout llFavorItem = holder.getView(R.id.ll_favor_item);
                final AppCompatTextView favorTitle = holder.getView(R.id.tv_favor_item_title);
                final AppCompatTextView favorUnit = holder.getView(R.id.tv_favor_item_unit);
                final AppCompatTextView favorPrice = holder.getView(R.id.tv_favor_item_price);
                final AppCompatImageView favorImg = holder.getView(R.id.image_favor_item);
                final Button delBtn = holder.getView(R.id.delete);

                favorTitle.setText(title);
                favorUnit.setText(unit);
                favorPrice.setText(price+"");
                Glide.with(holder.itemView.getContext()).load(img).apply(OPTIONS).into(favorImg);

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        holder.getView(R.id.itv_favor_left_slide).setVisibility(View.INVISIBLE);
                    }
                }, 5000);

                delBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        RestClient.builder()
                                .url("deleteFavoriteById")
                                .loader(favorDele.getContext())
                                .params("uuid", AccountManager.getSignUUID())
                                .params("id",favoriteId)
                                .success(new ISuccess() {
                                    @Override
                                    public void onSuccess(String response) {
                                        LatteLogger.e("response=",response);
                                        if (JSON.parseObject(response).getInteger("code") == 1) {
                                            favorDele.updateRecyclerView(response);
                                            Toasty.normal(favorDele.getContext(), "删除成功!").show();
                                        } else {
                                            Toasty.warning(favorDele.getContext(), "删除失败!").show();
                                        }
                                    }
                                }).build()
                                .post();
                    }
                });

                llFavorItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        favorDele.getSupportDelegate().startWithPop(IGoodsDetailDelegate.create(goodsId));
                    }
                });

                break;
            default:
                break;
        }
    }

    @Override
    public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
        return getData().get(position).getField(FavorItemFields.SPAN_SIZE);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    @Override
    public MultipleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_favor, parent, false);
        return new MultipleViewHolder(root);
    }
}
